<?php

/**
 * 后台基础控制器
 * @author yupoxiong<i@yufuping.com>
 */

namespace app\admin\controller;

use app\admin\model\AdminMenu;
use app\admin\model\AdminUser;
use app\admin\traits\{AdminAuth, AdminTree, PhpOffice};
use app\common\controller\BaseController;
use think\exception\HttpResponseException;
use think\Request;

class Controller  extends BaseController
{
    use AdminAuth, AdminTree, PhpOffice;

    /**
     * 当前用户ID
     * @var int
     */
    protected $uid = 0;

    /**
     * 当前用户
     * @var AdminUser
     */
    protected $user;

    //当前页码
    protected $page;

    //每页数据量
    protected $limit;
    /**
     * 后台变量
     * @var array
     */
    protected $admin;
    /**
     * 请求数据
     * @var array
     */
    protected $param;
    public $authExcept = [];

    //初始化基础控制器
    protected function initialize()
    {
        $request = $this->request;
        if ($this->isLogin()) {
            $this->checkOneDeviceLogin();
        } else {
            redirect(url('admin/auth/login')->build());
        }

        $this->param = $request->param();
        $this->page  = $this->param['page'] ?? 1;
        $this->limit = $this->param['limit'] ?? 10;
        $admin_per_page = cookie('admin_per_page') ?? 10;
        $this->admin['per_page'] = $admin_per_page < 100 ? $admin_per_page : 100;
        $this->admin['user'] = $this->user;
        $this->setAdminInfo();
        event('SystemLog', $this->user);
        $this->assign([
            'debug'         => env('APP_DEBUG') ? 'true' : 'false',
            'cookie_prefix' => config('cookie.prefix') ?? '',
            'admin'         => $this->admin,
        ]);
    }



    //空方法
    public function _empty()
    {
        $this->admin['name'] = '404';
        return $this->fetch('template/404');
    }


    //设置前台显示的后台信息
    protected function setAdminInfo(): void
    {
        $admin_config = config('admin.base');

        $this->admin['name']       = $admin_config['name'] ?? '';
        $this->admin['author']     = $admin_config['author'] ?? '';
        $this->admin['version']    = $admin_config['version'] ?? '';
        $this->admin['short_name'] = $admin_config['short_name'] ?? '';
        $this->admin['link']    = $admin_config['link'] ?? '';
    }
}
